package com.hzw.saas.api.old.storage.enums;

import java.util.Objects;

import com.hzw.saas.common.config.util.AssertUtil;


/**
 * 同名资源冲突解决方案
 *
 * @author zzl
 * @since 02/26/2021
 */
public enum ConflictModeEnum {

    RENAME_ORIGINAL(100, "RENAME_ORIGINAL"),   //重命名原资源
    COVER_EXISTS(200, "COVER_EXISTS"),         //覆盖目标资源，目标资源被覆盖删除
    RETURN_EXISTS(300, "RETURN_EXISTS"),       //保留目标资源，原资源被覆盖删除
    MERGE_EXISTS(301, "MERGE_EXISTS"),         //目录专用，合并目录子资源，保留目标目录，删除原目录
    THROW_EXISTS(400, "THROW_EXISTS"),         //抛出同名异常
    ALLOW_EXISTS(400, "ALLOW_EXISTS"),
    ;

    private final Integer code;
    private final String text;

    ConflictModeEnum(Integer code, String text) {
        this.code = code;
        this.text = text;
    }

    public Integer code() {
        return code;
    }

    public String text() {
        return text;
    }

    public static String text(Integer code) {
        for (ConflictModeEnum value : ConflictModeEnum.values()) {
            if (Objects.equals(value.code, code))
                return value.text;
        }
        return null;
    }

    public static ConflictModeEnum instance(Integer code) {
        return instance(code, false);
    }

    public static ConflictModeEnum instance(Integer code, boolean throwUnknown) {
        for (ConflictModeEnum value : ConflictModeEnum.values()) {
            if (Objects.equals(value.code, code))
                return value;
        }
        AssertUtil.assertThrow("存储空间代码异常，请设置正确的空间码", throwUnknown);
        return null;
    }

    /**
     * 获取实例
     *
     * @param cover 是否覆盖
     * @return cover==true?cover:throw
     */
    public static ConflictModeEnum instance(boolean cover) {
        return cover ? ConflictModeEnum.COVER_EXISTS : ConflictModeEnum.THROW_EXISTS;
    }
}
